<?php


namespace app\api\business;

use app\common\lib\TokenStr;
use app\common\model\mysql\User as userModel;
class User
{
    public $userObj = null;
    public function __construct()
    {
        $this->userObj = new userModel();
    }
    public function login($data){

        $redisCode = cache(config('redis.redis_phone').$data["phone_number"]);
        dump($redisCode);
        if(empty($redisCode) || $redisCode !=$data['code'] ){
            throw new \think\Exception("短信验证码不存在","-1000");
        }
        $user = $this->userObj->getUserByPhoneNumber($data["phone_number"]);

        if(!$user){
            $username = config("aip.username_default").$data["phone_number"];
            $userDate = [
                "username" => $username,
                "phone_number" => $data["phone_number"],
                "status" => config("status.mysql.table_normal"),
                "type" => $data['type']
            ];
            try {
                $this->userObj->save($userDate);
                $userId = $this->userObj->id;
            } catch (\Exception $e){
                throw new \think\Exception("数据库内部异常");
            };

        }else{
            $updateData = [
                "update_time" => time()
            ];
            $userId = $user->id;
            $username = $user->username;
            $updateRes = $this->userObj->updateById($userId,$updateData);
            if(!$updateRes){
                throw new \think\Exception("数据未更新");
            }

        }

        $token = TokenStr::getLoginToken($data["phone_number"]);
        $redisData = [
            "id" => $userId,
            "username" => $username
        ];
        $endTime = TokenStr::userLoginExpiresTime($data['type']);
        $redisRes =  cache(config("redis.token_default").$token,$redisData,$endTime);

        return $redisRes? ["token" => $token,"username" => $username]:false;
    }

    /**
     *
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByIdBus($id){
        $user = $this->userObj->getUserById($id);
        if(!$user || $user->status != config("status.mysql.table_normal")){
            return [];
        }
        return $user->toArray();
    }

    /**
     * @param $username
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserByUsernameBus($username){
        $user = $this->userObj->getUserByUsername($username);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }
    /**
     * @param $id
     * @param $date
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateUser($id,$date){
        //检查用户是否存在
        $user = $this->userObj->getUserById($id);
        if(!$user){
            throw new \think\Exception("不存在该用户");
        }
        //检查用户名是否存在
        $resUser = $this->getUserByUsernameBus($date["username"]);
        if(!$resUser && $resUser["id"] != $id){
            throw new \think\Exception("该用户已经存在请重复设置");
        }
        return $this->userObj->updateById($id,$date);

    }
}


















